<!-- 防止页面数据重复定义,模板数据统一在此 -->
<!--:
    var ableList = dicttag.getDictList('yes_no');
    var ableJson = commontag.toJson(ableList);
-->
<!-- widget grid -->
<div id="paramTable"></div>
<!-- end widget grid -->

<script type="text/javascript">

    /* DO NOT REMOVE : GLOBAL FUNCTIONS!
     *
     * pageSetUp(); WILL CALL THE FOLLOWING FUNCTIONS
     *
     * // activate tooltips
     * $("[rel=tooltip]").tooltip();
     *
     * // activate popovers
     * $("[rel=popover]").popover();
     *
     * // activate popovers with hover states
     * $("[rel=popover-hover]").popover({ trigger: "hover" });
     *
     * // activate inline charts
     * runAllCharts();
     *
     * // setup widgets
     * setup_widgets_desktop();
     *
     * // run form elements
     * runAllForms();
     *
     ********************************
     *
     * pageSetUp() is needed whenever you load a page.
     * It initializes and checks for all basic elements of the page
     * and makes rendering easier.
     *
     */
    pageSetUp();

    /*
     * ALL PAGE RELATED SCRIPTS CAN GO BELOW HERE
     */
    // PAGE RELATED SCRIPTS
    // pagefunction
    var pagefunction = function () {

        var params = {id: "${id}"};
        var $param_table = $('#paramTable');

        /**
         * 根据类型获取类型label
         * @param value 类型value
         */
        function getLabel(value) {
            var dicts = eval('${ableJson!""}');
            // 设置默认显示type
            var label = value;
            if (dicts) {
                $.each(dicts, function (i, dict) {
                    if (dict['value'] == value) {
                        label = dict['label'];
                        return false;
                    }
                });
            }
            return label;
        }

        var datatable = new DataTableData({
                    databind: $param_table.get(0),
                    //数据请求url地址
                    dataurl: '${ctx}/tools/param/list',
                    //数据请求参数
                    params: params,
                    hasCache: true,
                    hasCheck: true,
                    info: false,
                    alls: true,
                    checkBind: 'id',
                    toolbarStates: {
                        "add": {
                            action: function () {
                                form(-999);
                            }
                        },
                        "edit": {
                            action: function () {
                                var selDatas = datatable.getSelectedDatas();
                                if (selDatas && selDatas.length > 0) {
                                    form(selDatas[0]['id']);
                                }
                            }
                        },
                        "delete": {
                            mult: false,   // 不允许多选删除
                            action: function () {
                                var selDatas = datatable.getSelectedDatas();
                                if (selDatas && selDatas.length > 0) {
                                    var id = selDatas[0]['id'];
                                    layer.confirm('您真的要删除选择的数据？', {
                                                icon: 3,
                                                title: '删除参数',
                                                btn: ['确定', '取消'], //按钮
                                                shade: 0.4 //不显示遮罩
                                            }, function (index) {
                                                // send
                                                ajaxCoomand({
                                                    url: '${ctx}/tools/param/delete',
                                                    data: {id: id},
                                                    callback: function () {
                                                        datatable.refresh();
                                                        var treeObj = $.fn.zTree.getZTreeObj("paramTree");

                                                        var node = treeObj.getNodeByParam("id", "${id}");
                                                        if (node) {
                                                            treeObj.reAsyncChildNodes(node, "refresh");
                                                        }
                                                    }
                                                });

                                                layer.close(index);
                                            }
                                    );
                                }
                            }
                        },
                        "refresh": {
                            action: function () {
                                var treeObj = $.fn.zTree.getZTreeObj("paramTree");
                                var nodes = treeObj.getSelectedNodes();
                                if (nodes && nodes.length > 0) {
                                    datatable.refresh({id: nodes[0].id});
                                }
                            }
                        }
                    },
                    columns: [
                        {
                            data: 'label', title: '显示名称',
                            render: function (value, type, row) {
                                return '<a href="javascript:" onclick="form(' + "'" + row['id'] + "'" + ')">' + value + '</a>';
                            }
                        },
                        {data: 'value', title: '值'},
                        {data: 'name', title: '参数的Key'},
                        {
                            data: 'fixed', title: '锁定', width: '60px',
                            render: function (value) {
                                return '<div class="text-center"><span class="label label-default">' + getLabel(value) + '</span></div>';
                            }
                        },
                        {data: 'createDate', width: '140px', title: '创建时间', defaultContent: ''},
                        {data: 'description', width: '200px', title: '描述', defaultContent: ''}
                    ]
                }
        );

        /**
         * 修改任务
         */
        function form(id) {

            var treeObj = $.fn.zTree.getZTreeObj("paramTree");
            var node = treeObj.getNodeByParam("id", id);
            if (node) {
                if (id != -999 && id != "-999") {
                    node.isEditNode = true;
                }
                node.selectParentId = "${id}";
                treeObj.selectNode(node);
                $("#" + node.tId + "_a").click();
            }
        }

        // 注册
        window.form = form;

    };

    // destroy generated instances
    // pagedestroy is called automatically before loading a new page
    // only usable in AJAX version!
    var pagedestroy = function () {
        // 移除注册方法
        window.form = form;

    };
    // end destroy

    // load related plugins
    loadScript("/static/js/plugin/datatables/jquery.dataTables.js", function () {
        loadScript("/static/js/plugin/datatables/dataTables.colVis.js", function () {
            loadScript("/static/js/plugin/datatables/dataTables.tableTools.js", function () {
                loadScript("/static/js/plugin/datatables/dataTables.bootstrap.js", function () {
                    loadScript("/static/js/plugin/datatable-responsive/datatables.responsive.js", function () {
                        loadScript("/static/js/extend/DataTableData.js", pagefunction);
                    });
                });
            });
        });
    });

</script>